<template>
  <div class="app-container">
    <el-table :data="allList" stripe style="width: 100%">
      <el-table-column prop="name" label="类型">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间">
      </el-table-column>
      <el-table-column prop="updateTime" label="修改时间"> </el-table-column>
      <el-table-column prop="counts" label="总数">
      </el-table-column>
      <el-table-column fixed="right" label="操作" >
        <template slot-scope="scope">
          <el-button @click="editRow(scope.row)" type="text" size="small"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form"  label-width="80px">
        <el-row>
          <ImageUpload :parfileList = fileList :getFileList="getFileList" v-if="form.id == 1"></ImageUpload>
          <FileUpload :parfileList = fileList :getFileList="getFileList" v-if="form.id == 2"></FileUpload>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAll, listImages, listVideos, saveImages, saveVideos} from "@/api/system/videoImage";

export default {
  data() {
    return {
      loading: true,
      allList: [],
      // 表单参数
      form: {},
      title: '',
      fileList:'',
      open: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
     getList() {
      this.loading = true;
      listAll().then(response => {
        this.allList = response.data;
        this.loading = false;
      });
    },
    reset() {
      this.form = {
        id: undefined,
      };
      this.fileList = '';
      this.resetForm("form");
    },
    editRow(row) {
      this.reset();
      this.form.id = row.id;
      if(this.form.id === 1){
        listImages().then(response => {
        console.log(response)
        this.fileList = response.data.images;
        this.open = true;
        this.title = "相册管理";
      });
      }else if (this.form.id === 2){
        listVideos().then(response => {
          this.fileList = response.data.images;
          this.open = true;
          this.title = "视频管理";
        })
      }
      
    },
    getFileList(res){
      console.log(res,'video');
      this.fileList = res
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id === 1) { 
            saveImages({images:this.fileList}).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else if(this.form.id === 2){
            saveVideos({images:this.fileList}).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    }
  },
};
</script>

<style>
</style>